
import Database from "@tauri-apps/plugin-sql";

import { homePath, createDirs, getG01DataBase } from '../../utils';

import { DataSourceDomainMappingObject, DataSourceDomainMappingProperty } from '../types';


export const dataSourceDomainGetMappingObjectByObjIdentifier = async (dsId: number, objIdentifier: string): Promise<DataSourceDomainMappingObject | null> => {

    //
    if (dsId <= 0 || objIdentifier === '') {
        return null;
    }
    const db = await getG01DataBase();

    let items: Array<any> = await db.select(`select * from _g01_meta_object_database_class_mappings where data_source_id = $1 and object_identifier = $2 `, [dsId, objIdentifier]);

    if (items && items.length > 0) {
        //
        return items.map((item, index) => {
            return {
                id: item.id,
                dataSourceId: item.data_source_id,
                objectIdentifier: item.object_identifier,
                mappingType: item.mapping_type,
                objectSQL: item.object_sql,
                tableIdentifier: item.table_identifer,
                options: item.options,
                description: item.description,
            }
        })[0];
    }
    return null;
}


export const dataSourceDomainGetMappingPropertyByObjIdentifier = async (dsId: number, objIdentifier: string): Promise<Array<DataSourceDomainMappingProperty>> => {

    //
    if (dsId <= 0 || objIdentifier === '') {
        return [];
    }
    const db = await getG01DataBase();

    let items: Array<any> = await db.select(`select * from _g01_meta_object_database_attribute_mappings where data_source_id = $1 and object_identifier = $2 `, [dsId, objIdentifier]);

    if (items && items.length > 0) {
        //
        return items.map((item, index) => {
            return {
                id: item.id,
                dataSourceId: item.data_source_id,
                objectIdentifier: item.object_identifier,
                propertyIdentifier: item.property_identifier,
                columnIdentifier: item.column_identifer,
                options: item.options,
                description: item.description,
            }
        });
    }
    return [];
}


export const dataSourceDomainSaveMappingObject = async (obj: DataSourceDomainMappingObject): Promise<number> => {

    //
    const db = await getG01DataBase();
    //
    if (obj.id > 0) {
        await db.execute(`update _g01_meta_object_database_class_mappings set mapping_type = $1, object_sql = $2, table_identifer = $3, options = $4, description = $5 where id = $6`,
            [obj.mappingType, obj.objectSQL, obj.tableIdentifier, obj.options, obj.description, obj.id]);
        return 0;
    } else {
        await db.execute(`insert into _g01_meta_object_database_class_mappings(data_source_id,object_identifier,mapping_type,object_sql,table_identifer,options,description)Values($1,$2,$3,$4,$5,$6,$7)`,
            [obj.dataSourceId, obj.objectIdentifier, obj.mappingType, obj.objectSQL, obj.tableIdentifier, obj.options, obj.description]);

        let res: Array<any> = await db.select('SELECT last_insert_rowid() nid');
        console.log(res);
        if (res && res.length > 0) {
            return res[0].nid;
        }
        return 0;
    }
}


export const dataSourceDomainSaveMappingProperty = async (obj: DataSourceDomainMappingProperty): Promise<number> => {

    //
    const db = await getG01DataBase();
    //
    if (obj.id > 0) {
        await db.execute(`update _g01_meta_object_database_attribute_mappings set column_identifer = $1, options = $2, description = $3 where id = $4`,
            [obj.columnIdentifier, obj.options, obj.description, obj.id]);
        return 0;
    } else {
        await db.execute(`insert into _g01_meta_object_database_attribute_mappings(data_source_id,object_identifier,property_identifier,column_identifer,options,description)Values($1,$2,$3,$4,$5,$6)`,
            [obj.dataSourceId, obj.objectIdentifier, obj.propertyIdentifier, obj.columnIdentifier, obj.options, obj.description]);

        let res: Array<any> = await db.select('SELECT last_insert_rowid() nid');
        console.log(res);
        if (res && res.length > 0) {
            return res[0].nid;
        }
        return 0;
    }
}